import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Set;

import static java.lang.Thread.sleep;

/**
 * @Author：徐志荣
 * @Date：2023/5/22 17:30
 */
//切换窗口
public class Test11 {
    public static void main(String[] args) throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        //之所以,没有找到,是因为:默认是在 打开的页面找元素
        //我们需要切换页面

        //通过 getWindowHandles  获取到所有窗口句柄
        //通过 getWindowHandle  获取到get 打开的窗口句柄

        Set<String> handles = webDriver.getWindowHandles();
        String target_handle = "";
        for (String handle:handles
             ) {
            target_handle = handle;

        }
        webDriver.switchTo().window(target_handle);
        sleep(3000);

        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();

    }
}
